﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Runtime.InteropServices;

namespace WFFS
{
    public partial class FrmMsg : Form
    {
        public string msg = "";
        public string picmsg = "";
        public FrmMsg()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.Hide();
            string x = this.Location.X.ToString();
            string y = this.Location.Y.ToString();
            WriteIniData("FrmMsg", "X", x, Application.StartupPath + @"\WFFS.ini");
            WriteIniData("FrmMsg", "Y", y, Application.StartupPath + @"\WFFS.ini");
        }

        private void FrmMsg_Paint(object sender, PaintEventArgs e)
        {
            if (msg == "" && picmsg == "")
            {
                return;
            }
            if (msg != "")
            {
                if (richTBInfoDetail.Text.Length == 0)
                {

                    richTBInfoDetail.Text = msg;// +Environment.NewLine;
                }
                else
                {
                    string ms = richTBInfoDetail.Text;
                    string ab = msg + Environment.NewLine + ms; //.Substring(0, msg.Length - 1)
                    richTBInfoDetail.Text = ab;
                }
                msg = "";
            }

            if (picmsg == "")
            {
                return;
            }

            string[] sum = picmsg.ToString().Split(';'); //对图标指令做循环
            string picmsgtemp = "";
            for (int i = 0; i < sum.Length; i++)
            {
                picmsgtemp = sum[i];

                Panel pan = new Panel();
                pan.Dock = DockStyle.Top;
                pan.Size = new System.Drawing.Size(180, 45);
                pan.Location = new Point(0, 0);
                this.panelPicOrder.Controls.Add(pan);

                PictureBox pb = new PictureBox();
                string[] attr = picmsgtemp.ToString().Split('：');
                PictureBox pb1 = new PictureBox();
                foreach (Control c in groupBoxPic.Controls)
                {
                    if (c.Name == attr[1])
                        pb1 = (PictureBox)c;
                }
                pb.BackgroundImage = pb1.BackgroundImage;

                pb.Size = new System.Drawing.Size(40, 40);
                pb.Location = new Point(0, 0);
                pb.Name = "pic";
                pan.Controls.Add(pb);

                Label lb = new Label();
                lb.Text = attr[0];
                lb.Location = new Point(45, 20);
                lb.Size = new Size(120, 20);
                pan.Controls.Add(lb);
                picmsg = "";
            }
        }

        private void btnClear_Click(object sender, EventArgs e)
        {
            richTBInfoDetail.Text = "";
            panelPicOrder.Controls.Clear();
        }

        private void FrmMsg_FormClosed(object sender, FormClosedEventArgs e)
        {
            string x = this.Location.X.ToString();
            string y = this.Location.Y.ToString();
            WriteIniData("FrmMsg", "X", x, Application.StartupPath + @"\WFFS.ini");
            WriteIniData("FrmMsg", "Y", y, Application.StartupPath + @"\WFFS.ini");
        }
        public static bool WriteIniData(string Section, string Key, string Value, string iniFilePath)
        {
            if (File.Exists(iniFilePath))
            {
                long OpStation = WritePrivateProfileString(Section, Key, Value, iniFilePath);
                if (OpStation == 0)
                {
                    return false;
                }
                else
                {
                    return true;
                }
            }
            else
            {
                return false;
            }
        }
        [DllImport("kernel32")]
        private static extern long WritePrivateProfileString(string section, string key, string val, string filePath);
    }
}
